//for-in语法
//用于遍历数组或对象的属性的循环

let arr = [1, 2, 3, 4, 5];
let sum = 0;
for (const key in arr) {
    //key类似于数组的下标 是用在数组时
    let element = arr[key];
    sum += element;
    console.log(key + ':' + arr[key]);
}
console.log(sum);

//学生对象
function fn1() {
    let student = {
        id: 'tx001',
        name: 'alen',
        gender: '女',
        age: '19',
    }

    for (let i in student) {
        console.log(i + ':' + student[i]);
    }
}
fn1()

function table() {
    for (let i = 1; i <= 9; i++) {
        for (j = 1; j <= i; j++) {
            let sum = j * i;
            document.write(`${j}*${i}=${sum}&nbsp&nbsp&nbsp&nbsp`);
        }
        document.write('<br/>');
    }
}
//table();

function prime() {
    let count = 0;
    for (let i = 2; i < 200; i++) {
        let j = 2;
        while (i % j != 0) {
            j++;
        }
        if (i === j) {
            document.write(`<div class='item'>${i}</div>`);
            count++;
            if (count % 10 === 0) {
                document.write('<br/>');
            }
        }
    }
}
//prime()

//输出n层等腰三角形
function fn2() {
    let n = prompt('请输入层高：');
    if (isNaN(n)) {
        alert('请输入数字');
        fn2();
    }
    n = parseInt(n);
    for (let i = 0; i < n; i++) {
        //倒直角三角形
        for (let j = 0; j < n - i - 1; j++) {
            document.write('<div class="items"></div>');
        }
        //等腰三角形
        for (let j = 0; j < 2 * i + 1; j++) {
            document.write(`<div class='items'>*</div>`);
        }
        document.write('<br/>');
    }
}
fn2()